package dao

import (
	"gitee.com/shuwenhe/shuwen/deleteItemByID/model"
	"gitee.com/shuwenhe/shuwen/deleteItemByID/util"
)

// 获取所有商品信息
func GetItems() ([]*model.Item, error) {
	sql := "select id,title,author,price,sales,stock,img_path from item"
	rows, err := util.Db.Query(sql)
	if err != nil {
		return nil, err
	}
	var items []*model.Item
	for rows.Next() {
		item := &model.Item{}
		rows.Scan(&item.ID, &item.Title, &item.Author, &item.Price, &item.Sales, &item.Stock, &item.ImgPath)
		items = append(items, item)
	}
	return items, nil
}

func DeleteItemByID(id int) error {
	sql := "delete from item where id = ?"
	_, err := util.Db.Exec(sql, id)
	if err != nil {
		return err
	}
	return nil
}
